<template>
  <div class="main flex-row">
    <div class="main-left">
      <div class="main-left-img">
        <img src="@/assets/images/course-01.png" alt />
      </div>
      <p>暂无课程</p>
      <div style="text-align: center;margin-top:50px;">
        <el-button type="primary" @click="showJoinDialog()">添加课程</el-button>
      </div>
    </div>
    <div class="main-right">
      <div class="title">
        <div class="title-left">推荐课程</div>
      </div>
      <div class="content">
        <div class="item" v-for="(item, index) in list" :key="index" @click="showJoinDialog(item.classId)">
          <img :src="item.picUrl" alt />
          <div class="text">
            <p class="text-p1">{{item.resourceLessonName}}</p>
            <p class="text-p2">
              共{{item.chapterNumber}}章节
              <span>{{item.studyPeopleNum}}人正在学习</span>
            </p>
          </div>
        </div>
      </div>
    </div>
    <el-dialog title="加入班级" :visible.sync="joinDialogVisible" width="500px">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" >
        <el-form-item label="班级id" prop="classId">
          <el-input v-model="ruleForm.classId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="学生姓名" prop="name">
          <el-input v-model="ruleForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="学生学号" prop="stuNo">
          <el-input v-model="ruleForm.stuNo" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="joinDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { recommendLesson, addClassByCode } from '@/api/student/index'

export default {
  name: 'NoList',
  data () {
    return {
      list: [],
      joinDialogVisible: false,
      ruleForm: {
        classId: '',
        name: '',
        stuNo: ''
      },
      rules: {
        classId: [{ required: true, message: '请输入班级id', trigger: 'blur' }],
        name: [{ required: true, message: '请输入学生姓名', trigger: 'blur' }],
        stuNo: [{ required: true, message: '请输入学生学号', trigger: 'blur' }]
      }
    }
  },
  mounted () {
    this.recommendLesson()
  },
  methods: {
    // 推荐课程
    async recommendLesson () {
      const res = await recommendLesson()
      if (res.code === '200') {
        this.list = res.data
      } else {
        this.$message.error(res.message)
      }
    },
    showJoinDialog (classId) {
      this.ruleForm.classId = classId
      this.ruleForm.name = ''
      this.ruleForm.stuNo = ''
      this.joinDialogVisible = true
    },
    submitForm (ruleForm) {
      this.$refs[ruleForm].validate(async valid => {
        if (valid) {
          const res = await addClassByCode(this.ruleForm)
          if (res.code === '200') {
            this.$message.success('添加成功')
            // 重新查看是否已加入课程
            this.$updateLesson()
          } else {
            this.$message.error(res.message)
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.main {
  width: 1200px;
  margin: 0 auto;
  margin-top: 30px;
  align-items: flex-start;
  .title {
    position: relative;
    display: flex;
    justify-content: space-between;
    padding: 20px 0 0 10px;
    box-sizing: border-box;
    .title-left {
      font-weight: 500;
      font-size: 24px;
      color: rgba(16, 29, 55, 1);
    }
    .title-right {
      font-size: 14px;
      font-weight: 500;
      color: rgba(16, 29, 55, 1);
      opacity: 0.6;
      line-height: 27px;
      cursor: pointer;
    }
    .title-right:hover {
      color: rgba(16, 29, 55, 1);
      opacity: 0.3;
    }
  }
  .main-left {
    width: 452px;
    padding: 20px;
    box-sizing: border-box;
    height: 621px;
    background: #ffffff;
    border: 1px solid rgba(229, 229, 229, 1);
    border-radius: 5px;
    .main-left-img {
      width: 235px;
      height: 106px;
      background-color: #000;
      margin: 0 auto;
      margin-top: 160px;
      img {
        height: 100%;
        width: 100%;
      }
    }
    p {
      font-size: 16px;
      font-weight: 400;
      color: rgba(16, 29, 55, 1);
      text-align: center;
      margin-top: 20px;
    }
  }
  .main-right {
    width: 729px;
    height: 623px;
    background: rgba(255, 255, 255, 1);
    border: 1px solid rgba(229, 229, 229, 1);
    border-radius: 5px;

    .content {
      display: flex;
      flex-wrap: wrap;
      margin-top: 10px;
      padding-left: 15px;
      .item {
        margin-right: 20px;
        width: 216px;
        height: 244px;
        border: 1px solid #e9e9e9;
        margin-top: 20px;
        cursor: pointer;
        img {
          width: 100%;
          height: 180px;
          display: block;
        }
        .text {
          padding: 10px 20px 20px 14px;
          .text-p1 {
            font-size: 18px;
            font-weight: bold;
            color: rgba(16, 29, 55, 1);
          }
          .text-p2 {
            font-size: 14px;
            font-weight: 500;
            color: rgba(145, 149, 154, 1);
            margin-top: 10px;
            span {
              float: right;
            }
          }
        }
      }
    }
  }
}
</style>
